var module = require('./lib/module.Base');
require('./lib/validator.set');
require('./lib/getcode.min');
$(document).ready(function($) {
    var yzmCode = '';
    _.extend(module.PageEventBase.prototype,{
        login:function ($page) {
            var s = this;
            // 显示隐藏密码
            $page.find('.eyes').showPass();
            // 密码登录验证
            $page.find("#login_pw").validate({
                rules: {
                    tel:{
                        required: true,
                        phone:true,
                        minlength:11
                    },
                    password: {
                        required: true,
                        minlength: 6
                    }
                },
                submitHandler:function(form){
                    $.ajax({
                        type:'post',
                        dataType:'json',
                        url:$(form).attr('action'),
                        data:{
                            userName:$(form).find('[name=tel]').val(),
                            passWord:$(form).find('[name=password]:visible').val(),
                            ex:'ex'
                        },
                        beforeSend:function (xhr) {
                            $.showPreloader('登录中...');
                        },
                        success: function(data) {
                            $.hidePreloader();
                            if(data.code == 1){
                                $.toast(data.message);
                                var returnUrl = $.getParams(window.location.search).return;
                                if(_.isEmpty(returnUrl)){
                                    window.location.href = '/index.html';
                                }else{
                                    window.location.href = returnUrl;
                                }
                            }else{
                                $.toast(data.message);
                            }
                        }
                    });
                }
            });

            // 获取验证码
            $page.find('.getcode').bindGetCode();

            // 验证码登录
            $page.find("#login_yzm").validate({
                errorClass: 'error-tip',
                errorElement: 'span',
                onfocusout:false,
                onkeyup:false,
                onclick:false,
                focusInvalid:false,
                focusCleanup:false,
                rules: {
                    tel:{
                        required: true,
                        phone:true,
                        minlength:11
                    },
                    yzm: {
                        required: true,
                        minlength: 6
                    }
                },
                messages:{
                    tel:{
                        required: '请填写手机号码',
                        phone:'请填写正确的11位手机号码',
                        minlength:'请填写正确的11位手机号码'
                    },
                    yzm:{
                        required:"请填写验证码",
                        minlength:'至少输入6位验证码'
                    }
                },
                showErrors:function(errorMap,errorList) {
                    if(!_.isEmpty(errorList)){
                        var errMsg = _.chain(errorList).map(function (err) {
                            return err.message;
                        }).value();
                        $.alert(_.join(errMsg,'<br>'));
                    }
                },
                submitHandler:function(form){
                    $.showPreloader('登录中...');
                    $(form).ajaxPostData({},function (data) {
                        $.hidePreloader();
                        if(data.code == 1){
                            $.toast(data.message);
                            // window.location.href = '/index.html';
                        }else{
                            $.toast(data.message);
                        }
                    });
                }
            });
        },
        forgetPW:function ($page) {
            // 获取验证码
            $page.find('.getcode').bindGetCode();
            // 转到修改密码
            $page.find("#forgetPWForm").validate({
                rules: {
                    tel:{
                        required: true,
                        phone:true,
                        minlength:11
                    },
                    yzm: {
                        required: true,
                        minlength: 6

                    }
                },
                submitHandler:function(form){
                    $(form).ajaxPostData({},function (data) {
                        $.hidePreloader();
                        if(data.code == 1){
                            yzmCode = $(form).find('input[name=code]').val();
                            $.router.load('#changePW',true);
                        }else{
                            $.toast(data.message);
                        }
                    });


                }
            });
        },
        changePW:function ($page) {
            // 显示隐藏密码
            $page.find('.eyes').showPass();
            $page.find("#changePWForm").validate({
                rules: {
                    newPassword: {
                        required: true,
                        minlength: 6
                    }
                },
                messages:{
                    newPassword:{
                        required:"请填写登录密码",
                        minlength:'至少输入6位密码'
                    }
                },
                submitHandler:function(form){
                    // console.log('yzmCode',yzmCode);
                    if(_.isEmpty(yzmCode)){
                        $.alert('验证码失效,请重新获取验证码.', function () {
                            $.router.load('#forgetPW',true);
                        });
                    }else{
                        $.showPreloader();
                        $.ajax({
                            type:'post',
                            dataType:'json',
                            url:$(form).attr('action'),
                            data:{
                                code:yzmCode,
                                newPassword:$(form).find("input[name=newPassword]").val(),
                                ex:'ex'
                            },
                            success: function(data) {
                                $.hidePreloader();
                                if(data.code == 1){
                                    $.router.load('#login',true);
                                }else{
                                    $.toast(data.message);
                                    $.router.load('#forgetPW',true);
                                }
                            }
                        });
                    }
                }
            });
        }
    });
    var EventList = new module.PageEventBase();
    var pageId = $('.page.page-current').attr('id');
    EventList.init($('.page.page-current'),pageId);
});